package 算法.leetcode.offer.lkh;

/**
 * @author lchenglong
 * @date 2022/4/20
 */
public class LKH33 {

    public static void main(String[] args) {
        int[] nums = new int[]{1,3,5};
        int target = 1;
        LKH33 instance = new LKH33();
        int search = instance.search(nums, target);
        System.out.println(search);
    }
    public int search(int[] nums, int target) {
        int n = nums.length;
        // 边界条件
        if (n == 0)return -1;
        if (n == 1)return nums[0]==target?0:-1;
        // 二分查找到最小值
        int low = 0,high = nums.length-1;
        while (low<=high){
            int mid = (high+low)/2;
            if (nums[mid] ==target){
                return mid;
            }
            if (nums[mid]<nums[0]){
                if (target > nums[mid] && target <= nums[n-1]){
                    // 在后半升序数组
                    low = mid +1;
                } else {
                    // 不在后半升序数组
                    high = mid -1;
                }
            } else {
                if (target >= nums[0] && target < nums[mid]){
                    // 在第一个升序数组
                    high = mid -1;
                } else {
                    // 不在第一个升序数组
                    low = mid +1;
                }
            }
        }
        return -1;
    }
}
